<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: xinglou <xinglou1@foxmail.com>
 * +----------------------------------------------------------------------
 * 创建日期：2015-06-24
 * +----------------------------------------------------------------------
 * 文件描述：系统页面管理
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class FormAction extends CommonAction
{

    /**
     * 页面数据显示
     */
    public function index()
    {
        
        // 条件检索
        if (! empty($_REQUEST['search_keyword'])) {
            $where['id|title|intro'] = array(
                'like',
                '%' . $this->_request('search_keyword') . '%'
            );
        }
        
        $model = new FormModel();
        // 分页处理
        import('ORG.Util.Page'); // 导入分页类
        $count = $model->where($where)->count(); // 查询满足要求的总记录数
        $p = $this->_page($model, 'seq'); // 分页所需信息
        $page = new Page($count, $p['listRows']); // 实例化分页类 传入总记录数和每页显示的记录数
        $data = $model->field("id,
 		                        seq,
								title,
								intro,
								status status_num,
 								get_field_dict_name('form', 'status', status) status,
 								get_user_name(operator) operator,
								update_date")
            ->where($where)
            ->order("`" . $p['order'] . "` " . $p['sort'])
            ->limit($page->firstRow . ', ' . $page->listRows)
            ->select();
        $this->assign('data', $data);
        $show = $page->show(); // 分页显示输出
        $this->assign('show', $show); // 绑定分页显示字符串
        $this->display();
    }

    public function add()
    {
        // 数据字典列表解析
        $this->assign('status_list', set_field_dict_list('form', 'status'));
        $this->display();
    }

    public function insert()
    {
        $model = new FormModel();
        if (false !== $model->create()) {
            if (false !== $model->add()) {
                redirect(__APP__ . '?g=Admin&m=Form&a=index');
            } else {
                $this->error('问卷添加失败！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    public function edit()
    {
        $id = $this->_request('id');
        $model = new FormModel();
        $data = $model->where("id = '" . $id . "'")->find();
        $this->assign('data', $data);
        // 数据字典列表解析
        $this->assign('status_list', set_field_dict_list('form', 'status'));
        $this->display();
    }

    /**
     * 更新问卷信息至数据库
     */
    public function update()
    {
        $data = $this->update_form($this->_request(), $msg);
        if (false !== $data) {
            redirect(__APP__ . '?g=Admin&m=Form&a=index');
        } else {
            $this->error($msg);
        }
    }

    public function update_form($post_data, &$msg)
    {
        $model = new FormModel();
        if (false !== $model->create($post_data, 0)) {
            if (false !== $model->save()) {
                $msg = '修改成功！';
                return true;
            } else {
                $msg = '修改失败！';
                return false;
            }
        } else {
            $msg = '修改失败！' . $model->getError();
            return false;
        }
    }

    public function delete()
    {
        $formIds = $this->_request('ids');
        if (false !== $this->delete_form($formIds, $msg)) {
            redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->error($msg);
        }
    }

    public function delete_form($formIds, &$msg)
    {
        if (! empty($formIds) && is_array($formIds)) {
            $model = new FormModel();
            $model->startTrans();
            $id = implode("','", $formIds); // 用','分割id数组，以便于拼装sql语句
            $id = "'" . $id . "'"; // 拼装sql语句，结果为'firstId','secondId',…
            if (false !== $model->where("id in(" . $id . ")")->delete()) {
                $msg = '删除成功！';
                $model->commit();
                return true;
            } else {
                $msg = '删除失败！';
                $model->rollback();
                return false;
            }
        } else {
            $msg = '您未选中任何记录！';
            return false;
        }
    }
}
?>